import qiskit
How Alice is winning a game of coin flips with a quantum coin?
A simple coin toss
from qiskit import *
# Create circuit with 1 quantum and 1 classical bit
= QuantumCircuit(1, 1) circuit
# Superposition
0) circuit.h(
<qiskit.circuit.instructionset.InstructionSet at 0x121246320>
# Measure quantum bit and store result in classical bit
0, 0) circuit.measure(
<qiskit.circuit.instructionset.InstructionSet at 0x121245270>
qiskit.__qiskit_version__
{'qiskit-terra': '0.24.1', 'qiskit-aer': '0.12.1', 'qiskit-ignis': None, 'qiskit-ibmq-provider': '0.20.2', 'qiskit': '0.43.2', 'qiskit-nature': None, 'qiskit-finance': None, 'qiskit-optimization': None, 'qiskit-machine-learning': None}
from qiskit import IBMQ
'Put your own unique Token here', overwrite=True) IBMQ.save_account(
IBMQ.load_account()
<AccountProvider for IBMQ(hub='ibm-q', group='open', project='main')>
'mpl') circuit.draw(
!pip install matplotlib
Requirement already satisfied: matplotlib in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (3.7.2)
Requirement already satisfied: contourpy>=1.0.1 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (1.1.0)
Requirement already satisfied: cycler>=0.10 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (4.41.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (1.4.4)
Requirement already satisfied: numpy>=1.20 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (1.23.5)
Requirement already satisfied: packaging>=20.0 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (23.1)
Requirement already satisfied: pillow>=6.2.0 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (10.0.0)
Requirement already satisfied: pyparsing<3.1,>=2.3.1 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (3.0.9)
Requirement already satisfied: python-dateutil>=2.7 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: six>=1.5 in /Users/prashantmudgal/env_q/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
= IBMQ.get_provider('ibm-q') provider
provider.backends()
[<IBMQSimulator('ibmq_qasm_simulator') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_lima') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_belem') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_quito') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_statevector') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_mps') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_extended_stabilizer') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_stabilizer') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_jakarta') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_manila') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibm_lagos') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibm_nairobi') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibm_perth') from IBMQ(hub='ibm-q', group='open', project='main')>]
= provider.get_backend('ibmq_quito') qcomp
= execute(circuit, qcomp, shots=2) job
from qiskit.tools.monitor import job_monitor
job_monitor(job)
Job Status: job has successfully run
import matplotlib
%matplotlib inline
from qiskit.tools.visualization import plot_histogram
= job.result() result
result
Result(backend_name='ibmq_quito', backend_version='1.1.42', qobj_id='0b1fa52f-66d7-4e93-9382-7526a2643f56', job_id='circuit-runner_ciqmqjgs80m10ues9450_fc83_0', success=True, results=[ExperimentResult(shots=2, success=True, meas_level=2, data=ExperimentResultData(counts={'0x0': 1, '0x1': 1}), header=QobjExperimentHeader(clbit_labels=[['c', 0]], creg_sizes=[['c', 1]], global_phase=0.7853981633974483, memory_slots=1, metadata={}, n_qubits=5, name='circuit-133', qreg_sizes=[['q', 5]], qubit_labels=[['q', 0], ['q', 1], ['q', 2], ['q', 3], ['q', 4]]), status=done)], date=2023-07-17 22:36:17+05:30, status=Successful completion, header=QobjHeader(backend_name='ibmq_quito', backend_version='1.1.42', _ibm_tracing_={'uber-trace-id': 'bc3bab6050ad75d80a8c8274aa83050d:7fadaf45e434d3e0:e4c09d084ec71b6:1'}), execution_id='009ddb12-24c4-11ee-86b0-b02628f7f59e', time_taken=1.6188080459999998, error=None, client_version={'qiskit': '0.43.2'})
= job.result().get_counts()
counts = "heads" if next(iter(counts.keys())) == "0" else "tails" result
The momemnt of truth: Measure the result of your coin toss on a quantum computer
print(f"Your quantum coin landed on: {result}")
Your quantum coin landed on: heads
In a game of coin toss, how can Alice make sure that she wins all the time?
The following algorithm is based on the following paper - https://arxiv.org/pdf/2108.06271.pdf
How to win a quantum coin flip?
1. Alice prepares her penny in a state in some box where no one can see it
2. Bob decides whether he wants to flip the penny, applies X gate or not
3. Alice applies any gate (Hadamard)
4. We measure that outcome was always the same as the initial state
import numpy as np
from qiskit import QuantumCircuit
from qiskit_ibm_runtime import QiskitRuntimeService, Estimator, Session
= QuantumCircuit(1, 1) circuit
'mpl') circuit.draw(
Alice decides what the initial state of her qubit will be, by default it is 0
# If she wants the initial state to be 1 then she applies x gate
= 1 # make it 0 for state 0
init
if init == 1:
0)
circuit.x(
Alice applies Hadamard gate to create a superposition
0) circuit.h(
<qiskit.circuit.instructionset.InstructionSet at 0x12081ead0>
Bob decides whether to flip the state of penny that Alice gave to him or leave it as it is
# If he flips then flip = True, otherwise set it to false
= True
flip
if flip == True:
0) circuit.x(
Alice applies Hadamard gate
0) circuit.h(
<qiskit.circuit.instructionset.InstructionSet at 0x12081e5f0>
0,0) circuit.measure(
<qiskit.circuit.instructionset.InstructionSet at 0x12081efe0>
'mpl') circuit.draw(
provider.backends()
[<IBMQSimulator('ibmq_qasm_simulator') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_lima') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_belem') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_quito') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_statevector') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_mps') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_extended_stabilizer') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQSimulator('simulator_stabilizer') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_jakarta') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibmq_manila') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibm_lagos') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibm_nairobi') from IBMQ(hub='ibm-q', group='open', project='main')>,
<IBMQBackend('ibm_perth') from IBMQ(hub='ibm-q', group='open', project='main')>]
= provider.get_backend('ibmq_qasm_simulator') qcomp
= execute(circuit, qcomp, shots=1) job
from qiskit.tools.monitor import job_monitor
job_monitor(job)
Job Status: job has successfully run
= job.result() result
= job.result().get_counts()
counts counts
{'1': 1}
= 0 if next(iter(counts.keys())) == "0" else 1 fin_result
Moment of truth, no matter what Bob does, Alice wins!
print(f"Alice started with {init} and at the end she got {fin_result}. Alice wins!" )
Alice started with 1 and at the end she got 1. Alice wins!